<?php
class inventory
{
	var $ipsclass;
	
	var $inventory;
	var $order_field;
	
	var $order = array(
							'item' => 'item',
							'icon' => '',
							'name' => '',
							'category' => '',
							'stock' => 'count_item',
							'time' => 'time_bought',
							'resell' => 'resell_price',
							'price' => 'price_bought',
							'expire' => 'expired',
						);
	var $order_by = array(
							'ASC' => 1,
							'DESC' => -1,
						);
	var $order_default = array(
									'item' => 'ASC',
									'icon' => 'ASC',
									'name' => 'ASC',
									'category' => 'ASC',
									'stock' => 'DESC',
									'time' => 'DESC',
									'resell' => 'DESC',
									'price' => 'DESC',
									'expire' => 'ASC',
								);
	
	function auto_run()
	{
		$this->ipsclass->member['inventory_total'] = 0;
		
		if(!isset($this->order[$this->ipsclass->input['order']]))
		{
			$this->ipsclass->input['order'] = 'stock';
		}
		if(!isset($this->order_by[$this->ipsclass->input['by']]))
		{
			$this->ipsclass->input['by'] = $this->order_default[$this->ipsclass->input['order']];
		}
		
		if($this->ipsclass->input['by'] == $this->order_default[$this->ipsclass->input['order']])
		{
			if($this->order_default[$this->ipsclass->input['order']] == 'ASC')
			{
				$this->order_default[$this->ipsclass->input['order']] = 'DESC';
			}
			else
			{
				$this->order_default[$this->ipsclass->input['order']] = 'ASC';
			}
		}
		
		uasort($this->ipsclass->market_class->inventory,array(&$this,'order'));
		$inventory_html = "";
		
		foreach($this->ipsclass->market_class->inventory as $item_id => $items)
		{
			$item_html = reset($items);
			$item_html['count_item'] = count($items);
			$this->ipsclass->member['inventory_total'] = $this->ipsclass->numbers->add($this->ipsclass->member['inventory_total'],$item_html['count_item']);
			$price = 0;
			$resell = 0;
			$temp_html = "";
			if($item_html['count_item'] > 1)
			{
				foreach($items as $item)
				{
					$price = $this->ipsclass->numbers->add($price,$item['price_bought']);
					$resell = $this->ipsclass->numbers->add($resell,$item['resell_price']);
					$item['time_bought'] = $this->ipsclass->get_date($item['time_bought'],'TINY');
					$item['price_bought'] = $item['price_bought'] <= 0 ? $this->ipsclass->lang['free'] : $this->ipsclass->numbers->format_currency($item['price_bought']);
					$item['resell_price'] = $item['resell_price'] <= 0 ? $this->ipsclass->lang['trash'] : $this->ipsclass->numbers->format_currency($item['resell_price']);
					$item['checker'] = $item['i_id'];
					$item['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item_id]->parse_single_description($item)));
					$item['expired'] = $item['expired'] == 0 ? $this->ipsclass->lang['never'] : $this->ipsclass->get_date($item['expired'],'TINY');
					$temp_html .= $this->ipsclass->compiled_templates['skin_market']->inventory_item_multiple_row($item);
				}
				$item_html['count_item'] = $this->ipsclass->do_number_format($item_html['count_item']);
				$item_html['price_bought_avg'] = $price <= 0 ? $this->ipsclass->lang['free'] : $this->ipsclass->numbers->format_currency($this->ipsclass->numbers->round($this->ipsclass->numbers->divide($price,$item_html['count_item']),2));
				$item_html['resell_price_avg'] = $resell <= 0 ? $this->ipsclass->lang['trash'] : $this->ipsclass->numbers->format_currency($this->ipsclass->numbers->round($this->ipsclass->numbers->divide($resell,$item_html['count_item']),2));
				$item_html['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item_id]->parse_group_description($items)));
				$inventory_html .= $this->ipsclass->compiled_templates['skin_market']->inventory_item_multiple($item_html,$temp_html);
			}
			else
			{
				$item_html['price_bought'] = $item_html['price_bought'] <= 0 ? $this->ipsclass->lang['free'] : $this->ipsclass->numbers->format_currency($item_html['price_bought']);
				$item_html['resell_price'] = $item_html['resell_price'] <= 0 ? $this->ipsclass->lang['trash'] : $this->ipsclass->numbers->format_currency($item_html['resell_price']);
				$item_html['time_bought'] = $this->ipsclass->get_date($item_html['time_bought'],'TINY');
				$item_html['expired'] = $item_html['expired'] == 0 ? $this->ipsclass->lang['never'] : $this->ipsclass->get_date($item_html['expired'],'TINY');
				$item_html['checker'] = $item_html['i_id'];
				$item_html['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item_id]->parse_single_description($item_html)));
				$inventory_html .= $this->ipsclass->compiled_templates['skin_market']->inventory_item_single($item_html);
			}
		}
		
		$show['market_inv_info'] = $this->ipsclass->market_class->toggle_show('market_inv_info');
		$show['market_inv_item'] = $this->ipsclass->market_class->toggle_show('market_inv_item');
		
		$this->ipsclass->nav[] = "<a href='".$this->ipsclass->market_class->base_url."CODE=inventory'>".$this->ipsclass->lang['inventory']."</a>";
		
		$this->ipsclass->output = $this->ipsclass->compiled_templates['skin_market']->inventory_page($show,$inventory_html,$this->order_default);
	}
	
	function order(&$a,&$b)
	{
		if($this->order[$this->ipsclass->input['order']] != '' && $this->ipsclass->input['order'] != 'stock')
		{
			uasort($a,array(&$this,'order_sub'));
			uasort($b,array(&$this,'order_sub'));
		}
		$a_compare = &reset($a);
		$b_compare = &reset($b);
		$a_compare['count_item'] = count($a);
		$b_compare['count_item'] = count($b);
		
		if($this->order[$this->ipsclass->input['order']] != '')
		{
			$ab = $this->ipsclass->numbers->compare($a_compare[$this->order[$this->ipsclass->input['order']]],$b_compare[$this->order[$this->ipsclass->input['order']]]);
			if($this->ipsclass->input['order'] == 'expire')
			{
				$_a = $this->ipsclass->numbers->compare($a_compare[$this->order[$this->ipsclass->input['order']]],0);
				$_b = $this->ipsclass->numbers->compare($b_compare[$this->order[$this->ipsclass->input['order']]],0);
				if(($ab > 0 || $_a == 0) && $_b > 0)
				{
					return 1 * $this->order_by[$this->ipsclass->input['by']];
				}
				elseif(($ab < 0 || $_b == 0) && $_a > 0)
				{
					return -1 * $this->order_by[$this->ipsclass->input['by']];
				}
				else
				{
					return $this->second_order($a,$b);
				}
			}
			if($ab > 0)
			{
				return 1 * $this->order_by[$this->ipsclass->input['by']];
			}
			elseif($ab < 0)
			{
				return -1 * $this->order_by[$this->ipsclass->input['by']];
			}
			else
			{
				return $this->second_order($a_compare,$b_compare);
			}
		}
		else
		{
			$translate = array('name'=>'item_name','icon'=>'icon');
			if($this->ipsclass->input['order'] == 'category')
			{
				$a_2 = $this->ipsclass->market_class->category_order[$this->ipsclass->cache['market_items'][$a_compare['item']]['category']];
				$b_2 = $this->ipsclass->market_class->category_order[$this->ipsclass->cache['market_items'][$b_compare['item']]['category']];
				if($a_2 < $b_2)
				{
					return 1 * $this->order_by[$this->ipsclass->input['by']];
				}
				elseif($a_2 > $b_2)
				{
					return -1 * $this->order_by[$this->ipsclass->input['by']];
				}
				else
				{
					return $this->second_order($a_compare,$b_compare);
				}
			}
			else
			{
				$a_2 = $this->ipsclass->cache['market_items'][$a_compare['item']][$translate[$this->ipsclass->input['order']]];
				$b_2 = $this->ipsclass->cache['market_items'][$b_compare['item']][$translate[$this->ipsclass->input['order']]];
				return strcasecmp($a_2,$b_2) * $this->order_by[$this->ipsclass->input['by']];
			}
		}
	}
	
	function order_sub($a,$b)
	{
		if($a[$this->order[$this->ipsclass->input['order']]] > $b[$this->order[$this->ipsclass->input['order']]])
		{
			return 1 * $this->order_by[$this->ipsclass->input['by']];
		}
		elseif($a[$this->order[$this->ipsclass->input['order']]] < $b[$this->order[$this->ipsclass->input['order']]])
		{
			return -1 * $this->order_by[$this->ipsclass->input['by']];
		}
		else
		{
			return 0;
		}
	}
	
	function second_order($a,$b)
	{
		if($a['count_item'] > $b['count_item'])
		{
			return -1;
		}
		elseif($a['count_item'] < $b['count_item'])
		{
			return 1;
		}
		$ab = $this->ipsclass->numbers->compare($a['price_bought'],$b['price_bought']);
		if($ab > 0)
		{
			return -1;
		}
		elseif($ab < 0)
		{
			return 1;
		}
		$ab = $this->ipsclass->numbers->compare($a['resell_price'],$b['resell_price']);
		if($ab > 0)
		{
			return -1;
		}
		elseif($ab < 0)
		{
			return 1;
		}
		$ab = $this->ipsclass->numbers->compare($a['time_bought'],$b['time_bought']);
		if($ab > 0)
		{
			return -1;
		}
		elseif($ab < 0)
		{
			return 1;
		}
		$c = strcasecmp($this->ipsclass->cache['market_items'][$a['item']]['item_name'],$this->ipsclass->cache['market_items'][$b['item']]['item_name']);
		if($c != 0)
		{
			return $c;
		}
		if($this->ipsclass->market_class->category_order[$this->ipsclass->cache['market_items'][$a['item']]['category']] < $this->ipsclass->market_class->category_order[$this->ipsclass->cache['market_items'][$b['item']]['category']])
		{
			return 1;
		}
		elseif($this->ipsclass->market_class->category_order[$this->ipsclass->cache['market_items'][$a['item']]['category']] > $this->ipsclass->market_class->category_order[$this->ipsclass->cache['market_items'][$b['item']]['category']])
		{
			return -1;
		}
		$c = strcasecmp($this->ipsclass->cache['market_items'][$a['item']]['icon'],$this->ipsclass->cache['market_items'][$b['item']]['icon']);
		if($c != 0)
		{
			return $c;
		}
		if($a['item'] > $b['item'])
		{
			return 1;
		}
		elseif($a['item'] < $b['item'])
		{
			return -1;
		}
		// Shuold never right here
		return 0;
	}
}
?>